package com.mxfcode.back.controller;

import com.mxfcode.back.service.BackResourceService;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.common.HostHolder;
import com.mxfcode.entity.BBSResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author moxiufeng
 * @date 2021/3/7 23:25
 */
@Controller
@RequestMapping("/back/resource")
public class BackResourceController {
    private static final Logger logger = LoggerFactory.getLogger(BackResourceController.class);

    @Autowired
    private BackResourceService backResourceService;

    @Autowired
    private HostHolder hostHolder;

    @PostMapping("resourceList")
    @ResponseBody
    public PageDataResult resourceList(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize){
        logger.info("获取资源列表");
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取服务类目列表
            pdr = backResourceService.getResourceList(pageNum ,pageSize);
            logger.info("资源列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("资源列表查询异常！", e);
        }
        return pdr;
    }

    @PostMapping("/setResource")
    @ResponseBody
    public Map<String,Object> setResource(BBSResource resource, String files, HttpSession session){
        logger.info("用户上传资源[新增或更新]！resource:" + resource);
        logger.info("用户上传资源[新增或更新]！files:" + files);
        Map<String,Object> data = new HashMap();
        if(resource.getId() == null){
            //新增资源
            data = backResourceService.addResource(resource,files);
        }else{
            //修改资源
            data = backResourceService.updateResource(resource,files);
        }
        return data;
    }

    @PostMapping("del")
    @ResponseBody
    public Map<String, Object> del(@RequestParam("id") Integer id) {
        logger.info("删除资源！id:" + id);
        Map<String, Object> data = backResourceService.del(id);
        return data;
    }
}
